package com.masterclient.app.businessmodule.minemodule.message.contract;

import com.eoner.baselib.enums.FCPageViewState;
import com.eoner.baselib.presenter.ThrowableConsumer;
import com.eoner.baselib.utils.toast.ToastUtil;
import com.eoner.managerlibrary.netsign.NetEnvManager;
import com.masterclient.app.httpservice.RetrofitUtil;
import com.eoner.baselibrary.bean.personal.MessageCenterBean;

import io.reactivex.Observable;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
* Created by TMVPHelper on 2018/05/22
*/
public class MessagePresenter extends MessageContract.Presenter{

    private boolean firstLoadSuc = false;

    @Override
    public void getMessageCenter() {

        if (!firstLoadSuc) {
            mView.baseShowPageLoading(false);
        }

        Observable<MessageCenterBean> observable = RetrofitUtil.getInstance().getMessageCenter(NetEnvManager.BASE_SURL+"1/customer/get_message_center.html");

        Disposable disposable = toSubscribe(observable, new Consumer<MessageCenterBean>() {
            @Override
            public void accept(@NonNull MessageCenterBean bean) throws Exception {
                mView.baseHiddenPageLoading();
                mView.baseRefreshPageState(FCPageViewState.PAGE_STATE_NORMAL);
                if("0".equals(bean.getCode())){
                    firstLoadSuc = true;
                    mView.showData(bean.getData());
                } else {
                    ToastUtil.showToast(SERVER_FAILED_STR);
                    if (!firstLoadSuc) {
                        mView.baseRefreshPageState(FCPageViewState.PAGE_STATE_NET_ERROR);
                    }
                }
            }
        }, new ThrowableConsumer() {
            @Override
            public void accept(Throwable o) throws Exception {
                super.accept(o);
                mView.baseHiddenPageLoading();
                ToastUtil.showToast(WEB_FAILED_STR);
                if (!firstLoadSuc) {
                    mView.baseRefreshPageState(FCPageViewState.PAGE_STATE_NET_ERROR);
                }
            }
        });

        mRxManager.add(disposable);
    }
}